package com.example.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.AutoLog;
import com.example.common.Result;
import com.example.domain.entity.Emp;
import com.example.domain.entity.Params;
import com.example.service.EmpService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/emp")
public class EmpController {

    private static final Logger log = LoggerFactory.getLogger(EmpController.class);

    @Autowired
    private EmpService empService;

    @PostMapping
    @AutoLog("增加或修改教师信息")
    public Result save(@RequestBody Emp emp) {
        if (emp.getId() == null) {
            empService.add(emp);
        } else {
            empService.update(emp);
        }
        return Result.success();
    }

    @GetMapping("/findAll")
    public Result findAll() {
        List<Emp> list = empService.findAll();
        return Result.success(list);
    }

    @GetMapping("/search")
    public Result findBySearch(Params params) {
        log.info("拦截器已放行，正式调用接口内部，查询教师信息");
        Page<Emp> info = empService.findBySearch(params);
        return Result.success(info);
    }

    @DeleteMapping("/{id}")
    @AutoLog("删除了一个教师信息")
    public Result delete(@PathVariable Integer id) {
        empService.delete(id);
        return Result.success();
    }


}
